function prompt(message){
    document.querySelector('#prompt').innerHTML = message;
    setTimeout(function(){
        document.querySelector('#prompt').innerHTML = "";
    },800);
}
var app = angular.module('article',[]);
app.controller('articleCon',function($scope,$http,$sce){
    var url = window.location.pathname;
    var articleId = window.location.search.split("=")[1];
    $scope.isShow = false;
    $scope.post = [];
    $http({
        url:"/article",
        method:"post",
        data:{articleId:articleId}
    }).success(function(data){
        $scope.post = data['post'];
        $scope.isShow = true;
        if(data['success']){
            $scope.post.content = $sce.trustAsHtml($scope.post.content);
            $http({
                url:'/getComments?articleId='+articleId,
                method:'get'
            }).success(function(data){
                console.log(data);
                $scope.comments = data['comments'];
            }).error(function(){

            });
        }else if(data['message']==='noTheArticle'){
            $scope.prompt = "没有这篇文章";
        }
    });

    $scope.commentDetail = '';
    $scope.mail = '';
    $scope.nickname = "";
    $scope.submit = function(commentDetail,mail,articleId,commentNum,nickname){
        var commentToAdd = {articleId:articleId,commentDetail:commentDetail,mail:mail,loveNum:0,loved:false,nickname:nickname};
        var mailReg = new RegExp(/^[^\._-][\w\.-]+@([A-Za-z0-9]+\.)+[A-Za-z]+$/);
        if(mailReg.test(mail)){
            $http({
                url:"/addComment",
                method:"post",
                data:commentToAdd
            }).success(function(data){
                console.log(data);
                if(data['success']){
                    prompt('评论成功');
                    $scope.comments.push(commentToAdd);
                }else{
                    prompt('评论失败');
                }
            }).error(function(data){
                console.log(data);
                if(data['prompt'] === 'mailErr'){
                    prompt('请输入正确邮箱');
                }
            });
        }else{
            prompt('请输入正确邮箱');
        }

    }
});

window.onload = function(){
    if( window.top == window.self ){

    }else{
        document.getElementById('header').style.display = 'none';
        document.body.style.paddingTop = '0em'
    }
};